from django.contrib import admin
from django.conf import settings
#from tjango.ajax_select import make_ajax_form
from tjango.ajax_select.admin import *
from tjango.ajax_select.fields import *
from django.http import HttpResponse

class ExtraAdmin(AjaxSelectAdmin):

    def __init__(self, model, admin_site, *args, **kwargs):
        if hasattr(self.__class__, "ordered_inlines"):
            a = []
            for i in self.__class__.ordered_inlines:
                if isinstance(i, tuple):
                    a.append(i[1])
                else:
                    a.append(i)
                    
            setattr(self, 'inlines', a)
        super(ExtraAdmin, self).__init__(model, admin_site, *args, **kwargs)
        self.Media.js.append('/extra_admin/%s/calculated.js' % model.__name__.lower())
        self.Media.js.append('/extra_admin/%s/general.js' %  self.__class__.__name__.lower())

    save_on_top = True
    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(ExtraAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        if hasattr(self, 'read_only_fields'):
            if db_field.name in self.read_only_fields:
                field.widget.attrs['readonly'] = 'readonly' 
                #field.widget.attrs['class'] = 'readonly ' + field.widget.attrs.get('class', '')
        return field

    def lookup_allowed(self, lookup, value=None):
        try:
            for f in self.lookup_fields:
                if lookup.startswith(f):
                    return True
        except:
            pass
        return super(ExtraAdmin, self).lookup_allowed(lookup, value)        

    def response_change(self, request, obj, *args, **kwargs):
        if request.REQUEST.has_key('_popup'):  
            return HttpResponse('''                                                                                           
                <script type="text/javascript">                                                                                
                opener.dismissAddAnotherPopup(window);                                                                        
               </script>''')  
        else:  
            return super(ExtraAdmin, self).response_change(request, obj, *args, **kwargs)  

    class Media:
        js = [
            "/media/external/jquery/jquery.js",
            "/media/external/jquery-autocomplete/jquery.autocomplete.js",
            "/media/external/ajax_select/js/ajax_select.js",
            ]
        css = {
               'all': ("/media/external/jquery-autocomplete/jquery.autocomplete.css",
                   '/extra_admin/media/styles/base_admin.css',
                   '/media/external/ajax_select/iconic.css')  
              }



# Esto no sirve:
class ExtraInlineAdmin(AjaxSelectAdmin):

#    def __init__(self,*args, **kwargs):
#        super(ExtraInlineAdmin, self).__init__(model, admin_site, *args, **kwargs)
#        self.Media.js.append('/extra_admin/%s/calculated.js' % model.__name__.lower())
#        self.Media.js.append('/extra_admin/%s/general.js' %  self.__class__.__name__.lower())

    class Media:
        js = [
            "/media/external/jquery/jquery.js",
            "/media/external/jquery-autocomplete/jquery.autocomplete.js",
            "/media/external/ajax_select/js/ajax_select.js",
            ]
        css = {
               'all': ("/media/external/jquery-autocomplete/jquery.autocomplete.css",
                   '/extra_admin/media/styles/base_admin.css',
                   '/media/external/ajax_select/iconic.css')  
              }
